package com.snake.springboot.custom.service.impl.fileoss;

import com.snake.springboot.custom.api.model.fileoss.FileOSSModel;
import com.snake.springboot.custom.api.model.fileoss.OSSDelModel;
import com.snake.springboot.custom.api.model.fileoss.OSSPoliciesModel;
import com.snake.springboot.custom.api.model.fileoss.OSSUrlModel;
import com.snake.springboot.custom.api.response.fileoss.OSSAccRsp;
import com.snake.springboot.custom.api.response.fileoss.OSSInnerPoliciesRsp;
import com.snake.springboot.custom.api.response.fileoss.OSSPoliciesRsp;
import com.snake.springboot.custom.api.service.fileoss.FileActionService;
import com.snake.springboot.custom.api.service.fileoss.FileOssService;
import com.snake.springboot.custom.api.service.fileoss.OSSInstanceService;
import com.snake.springboot.custom.dao.mybatis.entity.fileoss.FileRecordDO;
import com.snake.springboot.custom.dao.mybatis.mappers.fileoss.FileRecordMapper;
import com.snake.springboot.custom.service.assemble.fileoss.FileRecordAssemble;
import com.snake.springboot.custom.service.config.Properties;
import org.snake.common.code.SerialNumberGeneratorFactory;
import org.snake.common.exception.OperationException;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>文件操作动作
 * 类{@code FileActionServiceImpl} </br>
 * 创建时间2025/5/7 14:53
 * @author yangxt
 * @see
 */
@Service
public class FileActionServiceImpl implements FileActionService {
    private FileOssService fileOssService;
    private FileRecordMapper recordMapper;
    private OSSInstanceService ossInstanceService;

    @Autowired
    public FileActionServiceImpl(FileOssService fileOssService, FileRecordMapper recordMapper, OSSInstanceService ossInstanceService) {
        this.fileOssService = fileOssService;
        this.recordMapper = recordMapper;
        this.ossInstanceService = ossInstanceService;
    }

    /**
     * 查询文件
     * @param model
     * @return
     */
    @Override
    public OSSPoliciesRsp generatePolicies(OSSPoliciesModel model) {
        Integer fileSize = Integer.parseInt(model.getFileSize());
        if (fileSize > Properties.MAX_FILE_SIZE){
            throw new OperationException("文件大小超过限制500M");
        }
        return generatePoliciesCommon(model);
    }


    @Override
    public OSSPoliciesRsp generatePoliciesBig(OSSPoliciesModel model) {
        return generatePoliciesCommon(model);
    }


    public OSSPoliciesRsp generatePoliciesCommon(OSSPoliciesModel model) {
        FileOSSModel ossModel = fileOssService.queryUsableCustomId(model.getCustomId());
        FileRecordDO recordDO = FileRecordAssemble.toRecordDO(ossModel,model);
        OSSPoliciesRsp rsp = ossInstanceService.generatePolicies(recordDO.getFileUrl(), ossModel);
        rsp.setAccessId(ossModel.getAccessKeyId());
        rsp.setFilePath(recordDO.getFileUrl());
        recordMapper.insertSelective(recordDO);
        return rsp;
    }
    /**
     * 删除文件
     * @param delModel
     */
    @Override
    @Async
    public void delOssUrls(OSSDelModel delModel) {
        if (ListUtils.isEmpty(delModel.getImgUrls())){
            return;
        }
        List<FileRecordDO> recordDOS = recordMapper.queryByUrlList(delModel.getImgUrls());
        if (ListUtils.isEmpty(recordDOS)){
            FileOSSModel ossModel = fileOssService.queryUsableCustomId(null);
            if (ossModel != null){
                ossInstanceService.delOssUrls(delModel,ossModel);
            }
        }else {
            for (FileRecordDO recordDO : recordDOS) {
                OSSDelModel model = new OSSDelModel(ListUtils.initObjList(recordDO.getFileUrl()));
                FileOSSModel ossModel = fileOssService.queryByOssId(recordDO.getFileOssId());
                if (ossModel != null){
                    ossInstanceService.delOssUrls(model,ossModel);
                }
            }
        }
        recordMapper.delByUrl(delModel.getImgUrls());
    }

    /**
     * 生成预签名url
     * @param urlModel
     * @return
     */
    @Override
    public String generatePresignedUrl(OSSUrlModel urlModel) {
        String fileUrl = urlModel.getFileUrl();
        if (fileUrl.startsWith("http")){
            return fileUrl;
        }
        if (!fileUrl.startsWith("/")){
            urlModel.setFileUrl("/"+fileUrl);
        }
        String ossId = recordMapper.queryByUrl(urlModel.getFileUrl());
        FileOSSModel ossModel = fileOssService.queryByOssId(ossId);
        if (ossModel == null){
            return urlModel.getFileUrl();
        }
        String presignedUrl = ossInstanceService.generatePresignedUrl(urlModel, ossModel);
        return presignedUrl;
    }

    @Override
    public OSSInnerPoliciesRsp innerGeneratePolicies(OSSPoliciesModel model) {
        FileOSSModel ossModel = fileOssService.queryUsableCustomId(model.getCustomId());
        FileRecordDO recordDO = FileRecordAssemble.toRecordDO(ossModel,model);
        recordMapper.insertSelective(recordDO);
        OSSAccRsp accRsp = new OSSAccRsp();
        PropertyUtils.copyProperties(accRsp,ossModel);
        OSSInnerPoliciesRsp rsp = new OSSInnerPoliciesRsp(recordDO.getFileUrl(),accRsp);
        return rsp;
    }

    @Override
    public String videoFirstFrame(OSSUrlModel urlModel) {
        String fileUrl = urlModel.getFileUrl();
        if (fileUrl.startsWith("http")){
            return fileUrl;
        }
        if (!fileUrl.startsWith("/")){
            urlModel.setFileUrl("/"+fileUrl);
        }
        String ossId = recordMapper.queryByUrl(urlModel.getFileUrl());
        FileOSSModel ossModel = fileOssService.queryByOssId(ossId);
        if (ossModel == null){
            return urlModel.getFileUrl();
        }
        String presignedUrl = ossInstanceService.videoFirstFrame(urlModel, ossModel);
        return presignedUrl;
    }
}
